package net.gyapp.markim.module.contact.adapter;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.easyder.wrapper.base.adapter.Selectable;

import net.gyapp.markim.R;
import net.gyapp.markim.core.entity.GroupEntity;
import net.gyapp.markim.core.entity.MemberEntity;
import net.gyapp.markim.module.basic.base.BaseRecyclerHolder;
import net.gyapp.markim.module.contact.vo.ContactHeaderEntity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Author:  winds
 * Data:    2018/7/7
 * Version: 1.0
 * Desc:
 */


public class InviteGroupAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseRecyclerHolder> implements Selectable<MemberEntity> {

    public final static int TYPE_CONTACT_HEADER = 3001;
    public final static int TYPE_CONTACT_USER = 3002;
    public final static int TYPE_CONTACT_GROUP = 3003;
    public List<MemberEntity> list = new ArrayList<>();

    public boolean isSingle = false;

    public void setSingleMode(boolean isSingle) {
        this.isSingle = isSingle;
    }

    public InviteGroupAdapter() {
        super(null);
        addItemType(TYPE_CONTACT_HEADER, R.layout.item_contact_cate);
        addItemType(TYPE_CONTACT_USER, R.layout.item_contact_content_invite);
        addItemType(TYPE_CONTACT_GROUP, R.layout.item_contact_content);
    }


    public void sort(List<MemberEntity> friendList) {
        // 排序
        Collections.sort(friendList, new Comparator<MemberEntity>() {
            @Override
            public int compare(MemberEntity lhs, MemberEntity rhs) {
                if (lhs.firstLetter.equals(rhs.firstLetter)) {
                    return lhs.memberId.compareTo(rhs.memberId);
                } else {
                    if ("#".equals(lhs.firstLetter)) {
                        return 1;
                    } else if ("#".equals(rhs.firstLetter)) {
                        return -1;
                    }
                    return lhs.firstLetter.compareTo(rhs.firstLetter);
                }
            }
        });
    }

    public int getLetterPosition(String letter) {
        int position = -1;
        for (int i = 0; i < mData.size(); i++) {
            MultiItemEntity entity = mData.get(i);
            if (entity instanceof ContactHeaderEntity) {
                if (((ContactHeaderEntity) entity).firstLetter.equalsIgnoreCase(letter)) {
                    return i;
                }
            }
        }
        return position;
    }

    @Override
    protected void convert(BaseRecyclerHolder helper, MultiItemEntity item) {
        switch (item.getItemType()) {
            case TYPE_CONTACT_HEADER:
                convertHeader(helper, (ContactHeaderEntity) item);
                break;
            case TYPE_CONTACT_USER:
                convertUser(helper, (MemberEntity) item);
                break;
            case TYPE_CONTACT_GROUP:
                convertGroup(helper, (GroupEntity) item);
                break;
        }
    }

    private void convertUser(BaseRecyclerHolder helper, MemberEntity item) {
        helper.setImageManager(mContext, R.id.iv_header, item.avatar, R.drawable.placeholder_header);
        helper.setText(R.id.tv_name, item.nick);
        helper.setImageResource(R.id.iv_selected, isSelected(item) ? R.drawable.invite_selected : R.drawable.invite_normal);
    }

    private void convertGroup(BaseRecyclerHolder helper, GroupEntity item) {
        helper.setText(R.id.tv_name, item.name);
        helper.setImageManager(mContext, R.id.iv_header, item.avatar, R.drawable.placeholder_group_header);
    }

    private void convertHeader(BaseRecyclerHolder helper, ContactHeaderEntity item) {
        helper.setText(R.id.tv_cate, item.name);
    }

    @Override
    public boolean isSelected(MemberEntity item) {
        return list.contains(item);
    }

    @Override
    public void toggleSelection(MemberEntity item) {
        if (isSelected(item)) {
            list.remove(item);
        } else {
            if (isSingle) {
                list.clear();
            }
            list.add(item);
        }
    }

    public boolean isSelected(MultiItemEntity entity) {
        return entity instanceof MemberEntity && isSelected((MemberEntity) entity);
    }

    public void toggleSelection(MultiItemEntity entity) {
        if (entity instanceof MemberEntity) {
            toggleSelection((MemberEntity) entity);
        }
    }

    @Override
    public void clearSelection() {
        list.clear();
    }

    @Override
    public int getSelectedCount() {
        return list.size();
    }

    @Override
    public List<MemberEntity> getSelection() {
        return list;
    }
}
